package com.css.bjdt_core.reportPrepare.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.css.bjdt_core.annual.entity.RptAnnual;
import com.css.bjdt_core.cell.dao.CellDataDao;
import com.css.bjdt_core.dnDzz.entity.DnDzz;
import com.css.bjdt_core.dnDzz.service.DnDzzService;
import com.css.bjdt_core.report.service.ReportTemplateManageService;
import com.css.bjdt_core.reportPrepare.entity.HigherLevelLssuedRange;
import com.css.bjdt_core.reportPrepare.entity.RptBatch;
import com.css.bjdt_core.reportPrepare.entity.RptBatchDzzRel;
import com.css.bjdt_core.reportPrepare.entity.RptBatchTemplateRel;
import com.css.bjdt_core.reportPrepare.entity.RptDzzChangeNews;
import com.css.bjdt_core.reportPrepare.entity.RptUnit;
import com.css.bjdt_core.reportPrepare.entity.RptUnitAnnualRel;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.util.TimeUtil;
import com.css.bjdt_core.validation.service.ValidationInfoService;

//报表下发处理逻辑
@Service
public class RptLssuedService {

	@Autowired
	private Dao dao;

	@Autowired
	private DnDzzService dnDzzService;

	@Autowired
	private RptDealWithService rptDealWithService;

	@Autowired
	private ReportTemplateManageService reportTemplateManageService;

	@Autowired
	private RptUnitService rptUnitService;

	@Autowired
	private ValidationInfoService validationInfoService;

	@Autowired
	private CellDataDao cellDataDao;

	public void rptLssuedMethod(String zzlb, String dzzId, List<String> templateIdsList, RptAnnual rptAnnual) {
		List<String> dzzIdsList = new ArrayList<String>();
		//如果是123，即北京党委，就是最高级党委，那么他下发报表给136和211
		/*if (zzlb.equals("123")) {
			dzzIdsList = dnDzzService.getLowerDzzGy(dzzId);//党组织下级
			dzzIdsList.add(dzzId);
			reportRelationshipHandle(dzzIdsList, templateIdsList, rptAnnual);
		} else if (zzlb.equals("136") || zzlb.equals("221")) {
			//如果是136或者211就需要查询出它的所有下级党组织
			dzzIdsList = dnDzzService.getAllChildByDzzIdContainBranch(dzzId);
			reportRelationshipHandle(dzzIdsList, templateIdsList, rptAnnual);
		}
		//后续再有啥情况再补
		else {
		}*/
		/*dzzIdsList.add("E9565E26-44ED-CEC5-39DF-B484E84DC500");
		dzzIdsList.add("94273476-113A-42AD-9405-76224D56CF24");
		dzzIdsList.add("FC4CD8E0-5CD1-4572-B292-99582E9773DC");
		dzzIdsList.add("7D6718AE-6A6A-4F88-91DD-5E5D88561DC0");
		dzzIdsList.add("846C0AE0-EFF2-4E12-B8A2-5C16B76165DD");
		dzzIdsList.add("D7092D28-428F-9AA7-60AC-CAAE007986DD");*/
		//如果是136或者211就需要查询出它的所有下级党组织
		dzzIdsList = dnDzzService.getAllChildByDzzIdContainBranch(dzzId);
		reportRelationshipHandleLinShi(dzzIdsList, templateIdsList, rptAnnual);
	}

	//报表关系处理方法
	@Transactional
	public void reportRelationshipHandle(List<String> dzzIdsList, List<String> templateIdsList, RptAnnual rptAnnual) {
		for (String dzzId : dzzIdsList) {
			RptBatchDzzRel rptBatchDzzRelOld = rptUnitService.queryByDzzIdAnnualId(dzzId, rptAnnual.getAnnualId());
			if (null == rptBatchDzzRelOld) {
				//先获得当前党组织的批次ID，如果没有就新建一套，如果有直接设置报表范围
				//先保存批次
				RptBatch rptBatch = new RptBatch();
				String batchId = CommonUtil.getUUID();
				rptBatch.setId(batchId);
				rptBatch.setStartTime(rptAnnual.getStartTime());
				rptBatch.setEndTime(rptAnnual.getEndTime());
				rptBatch.setReportState("0");
				dao.insert(rptBatch);
				//保存批次和党组织的关系
				RptBatchDzzRel rptBatchDzzRel = new RptBatchDzzRel();
				rptBatchDzzRel.setId(batchId);
				rptBatchDzzRel.setDzzId(dzzId);
				rptBatchDzzRel.setAnnualId(rptAnnual.getAnnualId());
				dao.insert(rptBatchDzzRel);
				rptDealWithService.clearBatchTemplateRel(batchId);
				//保存报表和批次之间的关系
				List<RptBatchTemplateRel> rptUnitTemplateRelList = new ArrayList<RptBatchTemplateRel>();
				//上级单位下发报表的范围
				List<HigherLevelLssuedRange> higherLevelLssuedRangeList = new ArrayList<HigherLevelLssuedRange>();
				for (String templateId : templateIdsList) {
					RptBatchTemplateRel rptUnitTemplateRel = new RptBatchTemplateRel();
					//根据当前报表的ID，得到套表的ID
					rptUnitTemplateRel
							.setGroupId(reportTemplateManageService.getAViewRptGroupTemplate(templateId).getGroupId());
					rptUnitTemplateRel.setTemplateId(templateId);
					rptUnitTemplateRel.setId(batchId);
					rptUnitTemplateRel.setValidationState("0");
					rptUnitTemplateRelList.add(rptUnitTemplateRel);

					//将上级单位下发的报表范围存入到上级单位报表范围表李
					HigherLevelLssuedRange higherLevelLssuedRange = new HigherLevelLssuedRange();
					higherLevelLssuedRange.setBatchId(batchId);
					higherLevelLssuedRange
							.setGroupId(reportTemplateManageService.getAViewRptGroupTemplate(templateId).getGroupId());
					higherLevelLssuedRange.setTemplateId(templateId);
					higherLevelLssuedRangeList.add(higherLevelLssuedRange);
				}
				dao.insert(rptUnitTemplateRelList);
				//保存上级单位下发的报表范围
				dao.insert(higherLevelLssuedRangeList);
			} else {
				rptDealWithService.clearBatchTemplateRel(rptBatchDzzRelOld.getId());
				//删除上级单位给下级单位下发的报表范围
				dao.clear(HigherLevelLssuedRange.class, Cnd.where("BATCH_ID", "=", rptBatchDzzRelOld.getId()));
				//保存报表和批次之间的关系
				List<RptBatchTemplateRel> rptUnitTemplateRelList = new ArrayList<RptBatchTemplateRel>();
				//上级单位下发报表的范围
				List<HigherLevelLssuedRange> higherLevelLssuedRangeList = new ArrayList<HigherLevelLssuedRange>();
				for (String templateId : templateIdsList) {
					RptBatchTemplateRel rptUnitTemplateRel = new RptBatchTemplateRel();
					//根据当前报表的ID，得到套表的ID
					rptUnitTemplateRel
							.setGroupId(reportTemplateManageService.getAViewRptGroupTemplate(templateId).getGroupId());
					rptUnitTemplateRel.setTemplateId(templateId);
					rptUnitTemplateRel.setId(rptBatchDzzRelOld.getId());
					rptUnitTemplateRel.setValidationState("0");
					rptUnitTemplateRelList.add(rptUnitTemplateRel);

					//将上级单位下发的报表范围存入到上级单位报表范围表李
					HigherLevelLssuedRange higherLevelLssuedRange = new HigherLevelLssuedRange();
					higherLevelLssuedRange.setBatchId(rptBatchDzzRelOld.getId());
					higherLevelLssuedRange
							.setGroupId(reportTemplateManageService.getAViewRptGroupTemplate(templateId).getGroupId());
					higherLevelLssuedRange.setTemplateId(templateId);
					higherLevelLssuedRangeList.add(higherLevelLssuedRange);
				}
				dao.insert(rptUnitTemplateRelList);
				//保存上级单位下发的报表范围
				dao.insert(higherLevelLssuedRangeList);
				RptDzzChangeNews rptDzzChangeNews = new RptDzzChangeNews();
				String newInfo = "报表上报关系被修改，修改时间为" + TimeUtil.dateFormatString(new Date());
				rptDzzChangeNews.setDzzId(dzzId);
				rptDzzChangeNews.setAnnualId(rptAnnual.getAnnualId());
				rptDzzChangeNews.setNewInfo(newInfo);
				dao.insert(rptDzzChangeNews);
				rptDealWithService.updateBatchState(rptBatchDzzRelOld.getId(), "0", null);
			}
			//删除这个党组织的所有的校核信息
			validationInfoService.deleteValidationInfoByDzzId(rptAnnual.getAnnualId(), dzzId);
		}
	}

	public int getChangeCount(String dzzId, String annualId) {
		Sql sql = Sqls.create(
				"select count(*) from RPT_DZZ_CHANGE_NEWS t where t.DZZ_ID=@dzzId and t.ANNUAL_ID=@annualId and DEL=1");
		sql.params().set("dzzId", dzzId).set("annualId", annualId);
		sql.setCallback(Sqls.callback.integer());
		dao.execute(sql);
		return sql.getObject(Integer.class);
	}

	public void deleteChangeInfo(String dzzId, String annualId) {
		Sql sql = Sqls
				.create("update RPT_DZZ_CHANGE_NEWS set DEL = 0 where DZZ_ID=@dzzId and ANNUAL_ID=@annualId and DEL=1");
		sql.params().set("dzzId", dzzId).set("annualId", annualId);
		dao.execute(sql);
	}

	//报表下发的时候，将下级单位的RPT_UNIT表中PID与本单位进行关联
	public void changeLowerUnitPid(String dzzId, String annualId, String zzlb) {
		List<String> dzzIdsList = new ArrayList<String>();
		//先获得当前党组织的单位ID
		RptUnitAnnualRel rptUnitAnnualRel = rptUnitService.getARptUnitAnnualRelByDzzIdAnnualId(dzzId, annualId);
		//如果是123，即北京党委，就是最高级党委，那么他下发报表给136和211
		if (zzlb.equals("123")) {
			dzzIdsList = dnDzzService.getLowerDzzGy(dzzId);//党组织下级
		} else if (zzlb.equals("136") || zzlb.equals("221")) {
			//如果是136或者211就需要查询出它的所有下级党组织
			dzzIdsList = dnDzzService.getAllChildByDzzIdContainBranch(dzzId);
		}
		//后续再有啥情况再补
		else {
		}
		for (String dzzIdNew : dzzIdsList) {
			RptUnitAnnualRel rptUnitAnnualRelLow = rptUnitService.getARptUnitAnnualRelByDzzIdAnnualId(dzzIdNew,
					annualId);
			if (rptUnitAnnualRelLow != null) {
				RptUnit rptUnit = rptUnitService.queryById(rptUnitAnnualRelLow.getUnitId());
				if (rptUnit.getPid() == null && rptUnit.getPid().equals("")) {
					rptUnitService.updateUnitPid(rptUnit.getId(), rptUnitAnnualRel.getUnitId());
				}
			}
		}
	}

	/**
	 * 保存重新的报表范围
	 * @param batchId 批次
	 * @param templateIdsList 新的报表范围
	 * @param deleteTemplateId 要删除的报表ID
	 */
	@Transactional
	public void saveReportRangeAgainSetUp(String batchId, List<String> templateIdsList, List<String> deleteTemplateId,
			String dzzId, String annualId) {
		//把新的报表范围增加进去
		List<RptBatchTemplateRel> rptUnitTemplateRelList = new ArrayList<RptBatchTemplateRel>();
		for (String templateId : templateIdsList) {
			RptBatchTemplateRel rptUnitTemplateRel = new RptBatchTemplateRel();
			//根据当前报表的ID，得到套表的ID
			rptUnitTemplateRel
					.setGroupId(reportTemplateManageService.getAViewRptGroupTemplate(templateId).getGroupId());
			rptUnitTemplateRel.setTemplateId(templateId);
			rptUnitTemplateRel.setId(batchId);
			//先查一下这张报表原来的校核状态
			RptBatchTemplateRel rptBatchTemplateRelOld = rptDealWithService.getARptBatchTemplateRel(batchId,
					templateId);
			if (null != rptBatchTemplateRelOld) {
				if (rptBatchTemplateRelOld.getValidationState().equals("1")) {
					rptUnitTemplateRel.setValidationState("3");
				} else {
					rptUnitTemplateRel.setValidationState(rptBatchTemplateRelOld.getValidationState());
				}
			} else {
				rptUnitTemplateRel.setValidationState("0");
			}

			rptUnitTemplateRelList.add(rptUnitTemplateRel);
		}
		//首先先删除旧的报表范围
		rptDealWithService.clearBatchTemplateRel(batchId);
		//其次循环删除要删除的报表数据
		for (String templateId : deleteTemplateId) {
			cellDataDao.deleteCellDataByDzzIdAndTemplateId(annualId, dzzId, templateId);
		}
		dao.insert(rptUnitTemplateRelList);
		//删除这个党组织的所有的校核信息
		validationInfoService.deleteValidationInfoByDzzId(annualId, dzzId);
	}

	//报表关系处理方法(临时方法)
	@Transactional
	public void reportRelationshipHandleLinShi(List<String> dzzIdsList, List<String> templateIdsList,
			RptAnnual rptAnnual) {
		for (String dzzId : dzzIdsList) {
			DnDzz dnDzz = dnDzzService.getDnDzzDataByDzzId(dzzId);
			RptBatchDzzRel rptBatchDzzRelOld = rptUnitService.queryByDzzIdAnnualId(dzzId, rptAnnual.getAnnualId());
			if (null == rptBatchDzzRelOld) {
				//先获得当前党组织的批次ID，如果没有就新建一套，如果有直接设置报表范围
				//先保存批次
				RptBatch rptBatch = new RptBatch();
				String batchId = CommonUtil.getUUID();
				rptBatch.setId(batchId);
				rptBatch.setStartTime(rptAnnual.getStartTime());
				rptBatch.setEndTime(rptAnnual.getEndTime());
				rptBatch.setReportState("0");
				dao.insert(rptBatch);
				//保存批次和党组织的关系
				RptBatchDzzRel rptBatchDzzRel = new RptBatchDzzRel();
				rptBatchDzzRel.setId(batchId);
				rptBatchDzzRel.setDzzId(dzzId);
				rptBatchDzzRel.setAnnualId(rptAnnual.getAnnualId());
				dao.insert(rptBatchDzzRel);
				rptDealWithService.clearBatchTemplateRel(batchId);
				//保存报表和批次之间的关系
				List<RptBatchTemplateRel> rptUnitTemplateRelList = new ArrayList<RptBatchTemplateRel>();
				for (String templateId : templateIdsList) {
					RptBatchTemplateRel rptUnitTemplateRel = new RptBatchTemplateRel();
					//根据当前报表的ID，得到套表的ID
					rptUnitTemplateRel
							.setGroupId(reportTemplateManageService.getAViewRptGroupTemplate(templateId).getGroupId());
					rptUnitTemplateRel.setTemplateId(templateId);
					rptUnitTemplateRel.setId(batchId);
					rptUnitTemplateRel.setValidationState("0");
					rptUnitTemplateRelList.add(rptUnitTemplateRel);
				}
				dao.insert(rptUnitTemplateRelList);

				RptUnitAnnualRel rptUnitAnnualRel = rptUnitService.getARptUnitAnnualRelByDzzIdAnnualId(dzzId,
						rptAnnual.getAnnualId());
				if (null == rptUnitAnnualRel) {
					//单位的基本信息
					RptUnit rptUnit = new RptUnit();
					String unitId = CommonUtil.getUUID();
					rptUnit.setId(unitId);
					rptUnit.setFullName(dnDzz.getDzzmc());
					rptUnit.setName(dnDzz.getDzzjc());
					rptUnit.setUnitType("1");
					dao.insert(rptUnit);
					RptUnitAnnualRel rptUnitAnnualRelInsert = new RptUnitAnnualRel();
					rptUnitAnnualRelInsert.setUnitId(unitId);
					rptUnitAnnualRelInsert.setAnnualId(rptAnnual.getAnnualId());
					rptUnitAnnualRelInsert.setDzzId(dzzId);
					dao.insert(rptUnitAnnualRelInsert);
				}
			} else {
				rptDealWithService.clearBatchTemplateRel(rptBatchDzzRelOld.getId());
				//保存报表和批次之间的关系
				List<RptBatchTemplateRel> rptUnitTemplateRelList = new ArrayList<RptBatchTemplateRel>();
				for (String templateId : templateIdsList) {
					RptBatchTemplateRel rptUnitTemplateRel = new RptBatchTemplateRel();
					//根据当前报表的ID，得到套表的ID
					rptUnitTemplateRel
							.setGroupId(reportTemplateManageService.getAViewRptGroupTemplate(templateId).getGroupId());
					rptUnitTemplateRel.setTemplateId(templateId);
					rptUnitTemplateRel.setId(rptBatchDzzRelOld.getId());
					rptUnitTemplateRel.setValidationState("0");
					rptUnitTemplateRelList.add(rptUnitTemplateRel);
				}
				dao.insert(rptUnitTemplateRelList);
				RptDzzChangeNews rptDzzChangeNews = new RptDzzChangeNews();
				String newInfo = "报表上报关系被修改，修改时间为" + TimeUtil.dateFormatString(new Date());
				rptDzzChangeNews.setDzzId(dzzId);
				rptDzzChangeNews.setAnnualId(rptAnnual.getAnnualId());
				rptDzzChangeNews.setNewInfo(newInfo);
				dao.insert(rptDzzChangeNews);
				rptDealWithService.updateBatchState(rptBatchDzzRelOld.getId(), "0", null);
			}
			//删除这个党组织的所有的校核信息
			validationInfoService.deleteValidationInfoByDzzId(rptAnnual.getAnnualId(), dzzId);
		}
	}

	public void rptLssuedMethodShiWei(String zzlb, String dzzId, List<String> templateIdsList, RptAnnual rptAnnual) {
		List<String> dzzIdsList = new ArrayList<String>();
		dzzIdsList.add("EE6129C0-DD24-4D1E-9BA6-4E10CBBF2E57");
		reportRelationshipHandleLinShi(dzzIdsList, templateIdsList, rptAnnual);
	}
}
